package com.hospital.register.core.controller.admin;

import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.ScheduleService;
import com.hospital.register.dto.core.common.ScheduleDaysDto;
import com.hospital.register.dto.core.common.SchedulePageDto;
import com.hospital.register.vo.core.common.ScheduleDaysVo;
import com.hospital.register.vo.core.common.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Api(tags = "排班管理", description = "面向管理员")
@RestController
@RequestMapping("/admin/hosp/schedule")
@Validated
// @CrossOrigin
public class ScheduleAdminController {

        /**
         * 处理排班相关业务
         */
        @Autowired
        private ScheduleService scheduleService;

        @ApiOperation(value = "分页查询指定科室的排班规则")
        @ApiImplicitParams(value = {
                        @ApiImplicitParam(name = "page", value = "页码（从1开始）", dataTypeClass = Long.class, paramType = "path", required = true),
                        @ApiImplicitParam(name = "limit", value = "每页条数", dataTypeClass = Long.class, paramType = "path", required = true),
                        @ApiImplicitParam(name = "hoscode", value = "所属医院的编号", dataTypeClass = String.class, paramType = "path", required = true),
                        @ApiImplicitParam(name = "depcode", value = "科室编号", dataTypeClass = String.class, paramType = "path", required = true)
        })
        @GetMapping("/getScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
        public Result<ScheduleDaysVo> getScheduleRule(
                        @PathVariable @Min(value = 1L, message = "页码不可 < 1") Integer page,
                        @PathVariable @Range(min = 1L, max = 50, message = "每页长度必须在1到50之间") Integer limit,
                        @PathVariable @NotBlank(message = "医院编号不可为空") String hoscode,
                        @PathVariable @NotBlank(message = "科室编号不可为空") String depcode) {

                ScheduleDaysDto conditions = new ScheduleDaysDto();
                conditions.setPage(page);
                conditions.setLimit(limit);
                conditions.setHoscode(hoscode);
                conditions.setDepcode(depcode);

                ScheduleDaysVo resultData = this.scheduleService.findDataOfDays(conditions);
                return Result.ok(resultData);
        }

        @ApiOperation(value = "查看指定日期的排班数据")
        @ApiImplicitParams(value = {
                        @ApiImplicitParam(name = "hoscode", value = "医院的编号", dataTypeClass = String.class, paramType = "path", required = true),
                        @ApiImplicitParam(name = "depcode", value = "科室的编号", dataTypeClass = String.class, paramType = "path", required = true),
                        @ApiImplicitParam(name = "workDate", value = "日期", dataTypeClass = String.class, paramType = "path", required = true),
        })
        @GetMapping("/getScheduleDetail/{hoscode}/{depcode}/{workDate}")
        public Result<List<ScheduleQueryVo>> getScheduleDetail(
                        @PathVariable @NotBlank(message = "医院编号不可为空") String hoscode,
                        // @PathVariable @NotBlank(message = "科室编号不可为空")
                        String depcode,
                        // @NotBlank(message = "日期不可为空")
                        @PathVariable @Pattern(regexp = "^(\\d{4})\\-(\\d{2})\\-(\\d{2})$", message = "日期的格式应为 2024-01-01") String workDate) {

                DateTime dateTime = new DateTime(workDate);
                SchedulePageDto conditions = new SchedulePageDto();
                conditions.setPage(1);
                conditions.setLimit(20);
                conditions.setHoscode(hoscode);
                conditions.setDepcode(depcode);
                conditions.setWorkDate(dateTime.toDate());

                Page<ScheduleQueryVo> page = this.scheduleService.findPage(conditions);
                return Result.ok(page.getContent());
        }

}
